/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgAddSpecialization.java
 *
 * Created on Aug 18, 2012, 4:37:51 PM
 */

package Model.dialog;

import Processing.dbclass.Specialization;
import DataBase.DBManagerment;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgEditSpecialization extends javax.swing.JDialog {

    /** Creates new form dlgAddSpecialization */
    public dlgEditSpecialization(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

     /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel46 = new javax.swing.JLabel();
        jLabel47 = new javax.swing.JLabel();
        txtSpecializationAddName = new javax.swing.JTextField();
        txtSpecializationAddID = new javax.swing.JTextField();
        btnSpecializationAction = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Specialization");

        jLabel46.setText("Specialization's ID:");

        jLabel47.setText("Specialization's Name:");

        txtSpecializationAddName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSpecializationAddNameActionPerformed(evt);
            }
        });

        txtSpecializationAddID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSpecializationAddIDActionPerformed(evt);
            }
        });

        btnSpecializationAction.setText("Edit");
        btnSpecializationAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSpecializationActionActionPerformed(evt);
            }
        });

        jButton9.setText("Cancel");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel46)
                            .addComponent(jLabel47))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSpecializationAddID, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSpecializationAddName, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(btnSpecializationAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton9)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel46)
                    .addComponent(txtSpecializationAddID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSpecializationAddName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel47))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSpecializationAction)
                    .addComponent(jButton9))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSpecializationActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSpecializationActionActionPerformed
        if (txtSpecializationAddID.getText().length() != 3) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Specialization's ID must have exactly three character!");
        }else if(db.searchSpecialization(con, 1, txtSpecializationAddID.getText()).getRowCount() != 0 && !txtSpecializationAddID.getText().equalsIgnoreCase(spec.getSpecialID())){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This Specialization's ID existed!");
        }else if (txtSpecializationAddName.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Specialization's Name must have!");
        }else{
            // Trong add Specialization này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            spec.setSpecialID(txtSpecializationAddID.getText());

            spec.setSpeciallName(txtSpecializationAddName.getText());

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
    }//GEN-LAST:event_btnSpecializationActionActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
         this.setVisible(false);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void txtSpecializationAddIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSpecializationAddIDActionPerformed
        // TODO add your handling code here:
        btnSpecializationAction.doClick();
    }//GEN-LAST:event_txtSpecializationAddIDActionPerformed

    private void txtSpecializationAddNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSpecializationAddNameActionPerformed
        // TODO add your handling code here:
        btnSpecializationAction.doClick();
    }//GEN-LAST:event_txtSpecializationAddNameActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgEditSpecialization dialog = new dlgEditSpecialization(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của Specialization
     */
    private Specialization spec = new Specialization();

    /**
     * Method trả về bill để chương trình xử lý
     * @return Specialization
     */
    public Specialization getSpecialization() {
        return spec;
    }

    public void setSpec(Specialization spec) {
        this.spec = spec;

        txtSpecializationAddID.setText(spec.getSpecialID());
        txtSpecializationAddName.setText(db.searchSpecialization(con, 1, spec.getSpecialID()).getValueAt(0, 1).toString());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSpecializationAction;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel46;
    private javax.swing.JLabel jLabel47;
    private javax.swing.JTextField txtSpecializationAddID;
    private javax.swing.JTextField txtSpecializationAddName;
    // End of variables declaration//GEN-END:variables

}
